package com.zdsoft.blp.zongbuju.shouye.linliquan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.youth.banner.Banner;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLShiChangActivitv;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.XQSCActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.SheQuLunTanAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.ZZLYadapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.PinCheEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/2.
 */
public class ESfragmentLeft extends CustomFragment implements NetCallback,XListView.IXListViewListener {
    private XListView xlv;
    private List<LinLi>list;
    private List<LinLi>mlist;
    private SheQuLunTanAdapter adapter;
    private int pageNo=1;
    private int source;
    private String touxiang,userName,fbTime,title,content,dianhua,imageContent,pingNum,id;
    @Override
    protected void init() {
        initLayout(R.layout.linli_fragment_es);
        xlv=find(R.id.xlv_ll_es);
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        LLShiChangActivitv ac= (LLShiChangActivitv) getActivity();
        source=ac.source;
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),XQSCActivity.class);
                intent.putExtra("id",list.get(position-1).getId());
                startActivity(intent);
            }
        });
        getNet();//实例化Net

    }

    public void getNet() {
        Net net=new Net();
        net.url= API.ESINFO_URL;
        net.nameSpace=API.ESINFO_NAMESPACE;
        Map map=new HashMap();
        if (source==2){
            net.methodName=API.MYESINFOLIST;//esInfoList
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("columnCode","");
            map.put("communityCode","00001");
            map.put("sellOrBuy","sell");
            map.put("condition","");
            map.put("userId","");
            map.put("curTime","");
            net.code=2;
            net.parms=map;
        }else {
            net.methodName=API.ESINFOLIST;//esInfoList
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("columnCode","");
            map.put("communityCode","00001");
            map.put("sellOrBuy","sell");
            map.put("condition","");
            map.put("curTime","");
            net.code=1;
            net.parms=map;
        }
        ((LLShiChangActivitv)getActivity()).getServer(net,this);
    }
    //回调函数+解析JOSN
    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("SSSSSSSS",res);
        if (flag){
            if (pageNo==1){
                mlist=jiexi(code,res);
                adapter = new SheQuLunTanAdapter(mlist, getActivity());
                xlv.setAdapter(adapter);
                xlv.stopRefresh();
            }else {
                list=jiexi(code,res);
                for (LinLi linLis:list){
                    mlist.add(linLis);
                }
                adapter.setListData(mlist);
                xlv.stopLoadMore();
            }
        }
    }

    private List<LinLi> jiexi(int code, String res) {
        list=new ArrayList<>();
        LinLi entity=null;
        try {
            JSONObject object=new JSONObject(res);
            boolean rows=object.isNull("rows");
            if (rows==false){
                JSONArray array=object.getJSONArray("rows");
                for (int i=0;i<array.length();i++){
                    JSONObject obj=array.getJSONObject(i);
                    userName=obj.getString("userName");
                    touxiang=obj.getString("headimgurl");
                    fbTime=obj.getString("createTime");
                    title=obj.getString("title");
                    content=obj.getString("content");
                    dianhua=obj.getString("contactPhone");
                    imageContent=obj.getString("picPath");
                    pingNum=obj.getString("replyNum");
                    id=obj.getString("id");
                    int approvedResult=obj.getInt("approvedResult");
                    if (code==1){
                        entity=new LinLi(dianhua,pingNum,imageContent,touxiang,fbTime,id,userName,title,content);
                        entity.setCode(2);
                    }
                    if (code==2){
                        entity=new LinLi(dianhua,pingNum,imageContent,touxiang,fbTime,id,userName,title,content,approvedResult);
                        entity.setCode(23);
                    }

                    list.add(entity);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        getNet();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNet();
    }
}
